using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Starznet.SharePoint.Controls.Web;

namespace Starznet.SharePoint.SPDAT.ApplicationPages.Settings
{
    public class SiteListTemplatesPage : StarznetBaseLayoutPage
    {
        protected GroupingRepeater RptTemplates;

        protected override void OnLoad(EventArgs e)
        {
            RptTemplates.GroupingColumn = "Feature";
            RptTemplates.IsGroupingDisabled = false;

            DataView oDv = PopulateData().DefaultView;
            oDv.Sort = "Feature ASC, DisplayName ASC";

            RptTemplates.DataSource = oDv;
            RptTemplates.DataBind();
        }

        private DataTable PopulateData()
        {
            DataTable oDT = new DataTable();
            oDT.Columns.Add(new DataColumn("DisplayName"));
            oDT.Columns.Add(new DataColumn("InternalName"));
            oDT.Columns.Add(new DataColumn("Unique"));
            oDT.Columns.Add(new DataColumn("Hidden"));
            oDT.Columns.Add(new DataColumn("SchemaXML"));
            oDT.Columns.Add(new DataColumn("Feature"));

            DataRow oRow = null;
            SPFeatureCollection oWebFeatures = SPContext.Current.Web.Features;
            SPFeatureCollection oSiteFeatures = SPContext.Current.Site.Features;

            foreach (SPListTemplate oTemplate in SPContext.Current.Web.ListTemplates)
            {
                oRow = oDT.NewRow();
                oRow["DisplayName"] = oTemplate.Name;
                oRow["InternalName"] = oTemplate.InternalName;
                oRow["Unique"] = (oTemplate.Unique) ? "True" : "False";
                oRow["Hidden"] = (oTemplate.Hidden) ? "True" : "False";
                oRow["SchemaXML"] = oTemplate.SchemaXml;

                SPFeature oFeature = null;
                oFeature = (oWebFeatures[oTemplate.FeatureId] == null) ? oSiteFeatures[oTemplate.FeatureId] : oWebFeatures[oTemplate.FeatureId];

                oRow["Feature"] = oFeature.Definition.DisplayName;

                oDT.Rows.Add(oRow);
            }
            return oDT;
        }
    }
}
